package com.xjtu.platform.admin.controller;

import com.xjtu.platform.admin.BjyAdminService;
import com.xjtu.platform.admin.dto.bjyDto.AddRuleDto;
import com.xjtu.platform.admin.dto.bjyDto.BorrowStaticDto;
import com.xjtu.platform.admin.entity.PenaltyRule;
import com.xjtu.platform.common.Constants;
import com.xjtu.platform.common.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin_2")
public class BjyAdminController {

    @Resource
    private BjyAdminService bjyAdminService;

    @PostMapping("/get_borrow_static")
    public Result getBorrowStatic(@RequestBody BorrowStaticDto borrowStaticDto){
        try {
            Map<String, Object> map = bjyAdminService.getBorrowStatic(borrowStaticDto);
            return Result.success("成功",map);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail(Result.ERR_CODE_SYS, Constants.ERROR_MESSAGE);
        }

    }

    @GetMapping("/get_rule")
    public Result getPenaltyRule(){
        try {
            List<PenaltyRule> list = bjyAdminService.getPenaltyRule();
            return Result.success("成功",list);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail(Result.ERR_CODE_SYS, Constants.ERROR_MESSAGE);
        }

    }

    @PostMapping("/add_rule")
    public Result addPenaltyRule(@RequestBody AddRuleDto addRuleDto){
        boolean isAdd = bjyAdminService.addRule(addRuleDto);
        if(isAdd){
            return Result.success("success");
        }else {
            return Result.fail(Result.ERR_CODE_SYS, Constants.ERROR_MESSAGE);
        }

    }

    @PostMapping("/delete_rule")
    public Result deleteRole(@RequestParam("penalty_id") String id){
        boolean isDelete = bjyAdminService.deleteRole(id);
        if(isDelete){
            return Result.success("success");
        }else {
            return Result.fail(Result.ERR_CODE_SYS, Constants.ERROR_MESSAGE);
        }
    }

}
